package com.huika.renrench.control.my.account;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.Response.Listener;
import com.google.gson.reflect.TypeToken;
import com.hk.app.android.lib.encrypt.MD5Security;
import com.huika.lib.validation.EditTextValidator;
import com.huika.lib.validation.ValidationModel;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.Constant;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.login.validations.NewPassWordValidation;
import com.huika.renrench.control.login.validations.PassWordValidation;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.utils.PreferencesHelper;
import com.huika.renrench.view.TitleBarHelper;
import com.huika.renrench.view.clearedit.ClearableEditText;

public class ModifyLoginPwdActivity extends BaseAct implements Listener<RequestResult<String>> {
	private ClearableEditText etYPwd;
	private ClearableEditText etNPwd;
	private ClearableEditText etCPwd;
	private Button btn;

	private EditTextValidator editTextValidator;
	private String newPwd;

	@Override
	public void setRootView() {
		setContentView(R.layout.activity_modify_loginpwd);
	}

	@Override
	protected void initWidget() {
		TitleBarHelper titleBarHelper = new TitleBarHelper(this, -1, -1, R.string.title_modify_pwd);
		titleBarHelper.setLeftMsg(R.string.left_back);
		etYPwd = (ClearableEditText) findViewById(R.id.et_ypwd_amlp);
		etNPwd = (ClearableEditText) findViewById(R.id.et_npwd_amlp);
		etCPwd = (ClearableEditText) findViewById(R.id.et_cpwd_amlp);
		etYPwd.addTextChangedListener(new MyPassWordWatcher(etYPwd));
		etNPwd.addTextChangedListener(new MyPassWordWatcher(etNPwd));
		etCPwd.addTextChangedListener(new MyPassWordWatcher(etCPwd));
		btn = (Button) findViewById(R.id.btn_amlp);
		btn.setOnClickListener(this);
		editTextValidator = new EditTextValidator(this).add(new ValidationModel(etNPwd, new NewPassWordValidation())).execute();
	}

	@Override
	public void widgetClick(View v) {
		modifyNet();
	}

	private void modifyNet() {
		String ypwdStr = getInputStr(etYPwd);
		if (ypwdStr == null || TextUtils.isEmpty(ypwdStr)) {
			showToastMsg("请输入原密码");
			return;
		}
		String npwStr = getInputStr(etNPwd);
		if (npwStr == null || TextUtils.isEmpty(npwStr)) {
			showToastMsg("请输入新密码");
			return;
		}
		if (!editTextValidator.validate()) {
			return;
		}
		String cpwStr = getInputStr(etCPwd);
		if (cpwStr == null || TextUtils.isEmpty(cpwStr)) {
			showToastMsg("请输入确认密码");
			return;
		}
		if (!npwStr.equals(cpwStr)) {
			showToastMsg("两次新密码输入不一致，请重新输入");
			return;
		}
		newPwd = MD5Security.getMd5_32_UP(npwStr);
		// 网络连接
		showLoadingDialog("");
		FormResultRequest<String> request = new FormResultRequest<String>(UrlConstants.CHANGELOGINPASSWORD, this, this, new TypeToken<RequestResult<String>>() {
		}.getType());
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		ajaxParams.putStringTypeParam("userId", GlobalApp.userModel.getUserId());
		ajaxParams.putStringTypeParam("oldPassword", MD5Security.getMd5_32_UP(ypwdStr));
		ajaxParams.putStringTypeParam("password", newPwd);
		ajaxParams.putStringTypeParam("ip", GlobalApp.ip);
		ajaxParams.putStringTypeParam("mac", GlobalApp.mac);
		ajaxParams.putStringTypeParam("dev", Constant.DEV);
		request.setRequestParams(ajaxParams);
		executeRequest(request);
	}

	class MyPassWordWatcher implements TextWatcher {
		private EditText editText;
		private String tempStr;

		public MyPassWordWatcher(EditText editText) {
			this.editText = editText;
		}

		@Override
		public void afterTextChanged(Editable s) {
			if (tempStr.indexOf(" ") != -1) {
				editText.setText(tempStr.trim());
				editText.setSelection(tempStr.length());
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			if (s.toString().equals(" ")) {
				editText.setText("");
			} else {
				tempStr = s.toString();
			}
		}

	}

	@Override
	public void onResponse(RequestResult<String> arg0) {
		dismissLoadingDialog();
		showToastMsg(arg0.msg);
		if (arg0.flag == 1) {
			PreferencesHelper.setString(PreferencesHelper.KEY_LOGIN_PWD, newPwd);
			this.finish();
		}
	}

}
